package zhaomh.zhlib.gui.widget;


import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.PressableWidget;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;
import zhaomh.zhlib.config.EnumValue;

import java.util.function.Supplier;

@Environment(EnvType.CLIENT)
public class EnumConfigWidget extends PressableWidget implements ConfigWidget<EnumValue> {
    protected static final NarrationSupplier DEFAULT_NARRATION_SUPPLIER = Supplier::get;
    protected final NarrationSupplier narrationSupplier;
    private final EnumValue value;

    public static Builder builder(EnumValue value) {
        return new Builder(value);
    }

    protected EnumConfigWidget(int x, int y, int width, int height, EnumValue value, NarrationSupplier narrationSupplier) {
        super(x, y, width, height, Text.of(value.getTr()));
        this.narrationSupplier = narrationSupplier;
        this.value = value;

    }

    @Override
    public EnumValue getValue() {
        return value;
    }

    @Override
    public void onPress() {
        value.next();
        setMessage(Text.of(value.getTr()));
    }

    @Override
    protected MutableText getNarrationMessage() {
        return this.narrationSupplier.createNarrationMessage(super::getNarrationMessage);
    }

    @Override
    public void appendClickableNarrations(NarrationMessageBuilder builder) {
        this.appendDefaultNarrations(builder);
    }

    @Environment(EnvType.CLIENT)
    public static class Builder {
        private final EnumValue value;
        @Nullable
        private Tooltip tooltip;
        private int x;
        private int y;
        private int width = 120;
        private int height = 20;
        private NarrationSupplier narrationSupplier = DEFAULT_NARRATION_SUPPLIER;

        public Builder(EnumValue value) {
            this.value = value;
        }

        public Builder position(int x, int y) {
            this.x = x;
            this.y = y;
            return this;
        }

        public Builder width(int width) {
            this.width = width;
            return this;
        }

        public Builder size(int width, int height) {
            this.width = width;
            this.height = height;
            return this;
        }

        public Builder dimensions(int x, int y, int width, int height) {
            return this.position(x, y).size(width, height);
        }

        public Builder tooltip(@Nullable Tooltip tooltip) {
            this.tooltip = tooltip;
            return this;
        }

        public Builder narrationSupplier(NarrationSupplier narrationSupplier) {
            this.narrationSupplier = narrationSupplier;
            return this;
        }

        public EnumConfigWidget build() {
            EnumConfigWidget buttonWidget = new EnumConfigWidget(this.x, this.y, this.width, this.height, this.value,this.narrationSupplier);
            buttonWidget.setTooltip(this.tooltip);
            return buttonWidget;
        }
    }

    @Environment(EnvType.CLIENT)
    public interface NarrationSupplier {
        MutableText createNarrationMessage(Supplier<MutableText> textSupplier);
    }
}